package com.api.shopapi.mapper;

import com.api.shopapi.entity.NursingItem;
import com.api.shopapi.entity.dto.NursingItemDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface NursingItemMapper {

    @Insert("insert into nursing_item " +
            "(NursingItemCode," +
            "Name," +
            "NursingTypeCode," +
            "Sort," +
            "Status," +
            "Duration," +
            "StaffCode," +
            "InstitutionCode) " +
            "values (" +
            "#{nursingItem.nursingItemCode}," +
            "#{nursingItem.name}," +
            "#{nursingItem.nursingTypeCode}," +
            "#{nursingItem.sort}," +
            "#{nursingItem.status}," +
            "#{nursingItem.Duration}," +
            "#{nursingItem.staffCode}," +
            "#{nursingItem.institutionCode} )")
    int insert(@Param("nursingItem") NursingItem nursingItem);

    @Update("update nursing_item" +
            " set" +
            " Name=#{nursingItem.name}," +
            " NursingTypeCode=#{nursingItem.nursingTypeCode}," +
            " Sort=#{nursingItem.sort}," +
            " Status=#{nursingItem.status}," +
            " Duration=#{nursingItem.Duration}," +
            " InstitutionCode=#{nursingItem.institutionCode}" +
            " where NursingItemCode=#{nursingItem.nursingItemCode} ")
    int update(@Param("nursingItem") NursingItem nursingItem);

    @Delete("delete from nursing_item where nursingItemCode=#{nursingItemCode} ")
    int delete(@Param("nursingItemCode") String nursingItemCode);

    @Select("select  * from nursing_item where nursingItemCode=#{nursingItemCode} limit 1")
    NursingItem getByNursingItemCode(@Param("nursingItemCode") String nursingItemCode);

    @Select("<script>" +
            "select  i.*, t.Name nursingTypeName from nursing_item i,nursing_type t " +
            "where i.InstitutionCode=#{institutionCode} and i.NursingTypeCode=t.NursingTypeCode " +
            "<if test ='name !=null and name!=\"\" '>" +
            "and i.Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='nursingTypeCode !=null and nursingTypeCode!=\"\" '>" +
            "and i.NursingTypeCode = #{nursingTypeCode} " +
            "</if>" +
            "<if test ='status !=-1 '>" +
            "and i.Status =#{status} " +
            "</if>" +
            "order by i.Sort desc" +
            "</script>")
    List<NursingItemDTO> getListByInstitutionCode(@Param("institutionCode") String institutionCode, @Param("name") String name, @Param("status") int status, @Param("nursingTypeCode") String nursingTypeCode);

    @Select("select  * from nursing_item where name =#{name} and institutionCode=#{institutionCode} limit 1")
    NursingItem getByNameAndInstitutionCode(@Param("name") String name, @Param("institutionCode") String institutionCode);

    @Select("select  * from nursing_item where institutionCode=#{institutionCode} and Status=0 order by Sort DESC")
    List<NursingItem> getList(@Param("institutionCode") String institutionCode);

    @Select("select  * from nursing_item where InstitutionCode=#{institutionCode} and NursingTypeCode=#{nursingTypeCode} and Status=0 order by Sort DESC")
    List<NursingItem> getListByNursingType(@Param("institutionCode") String institutionCode,@Param("nursingTypeCode") String nursingTypeCode);

    @Select("<script>" +
            "select * from nursing_item " +
            " where InstitutionCode in (select institutionCode from institution where governmentCode=#{governmentCode} )" +
            "order by Sort desc" +
            "</script>")
    List<NursingItem> listByGovCode(@Param("governmentCode") String governmentCode);


}
